package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.ItemNewVoteImageBinding;
import com.jwdf.uda.ui.fragment.question.bean.NewVote;
import com.jwdf.uda.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 小尛龙 on 2015/11/16 0016.
 */
public class RecyclerViewAdapters extends BaseAdapter<NewVote> {

    protected List<NewVote> mData =new ArrayList();

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new VotePlanImageViewHolder(mInflater.inflate(R.layout.item_new_vote_image, parent, false));
    }

    public RecyclerViewAdapters(Context context) {
        super(context);
    }

    public int getItemViewType(int position) {
       return position;
    }

    public void notifyDataSetChange(){
        notifyDataSetChanged();
    }


    public void setDetailData(List<NewVote> newVoteList) {
        mData.clear();

//        addUserItem(newVoteList);
        if (newVoteList.get(0).getUploadImages().size()>0) {
            addPlanImageItem(mContext.getString(R.string.vote_plan_title_a),newVoteList.get(0).getUploadImages(),newVoteList.get(0).getDescription() );
        }
        if (newVoteList.get(1).getUploadImages().size()>0) {
            addPlanImageItem(mContext.getString(R.string.vote_plan_title_b), newVoteList.get(1).getUploadImages(),newVoteList.get(1).getDescription());
        }
        if (newVoteList.get(2).getUploadImages().size()>0) {
            addPlanImageItem(mContext.getString(R.string.vote_plan_title_c), newVoteList.get(2).getUploadImages(),newVoteList.get(2).getDescription());
        }
        if (newVoteList.get(3).getUploadImages().size()>0) {
            addPlanImageItem(mContext.getString(R.string.vote_plan_title_c), newVoteList.get(3).getUploadImages(),newVoteList.get(3).getDescription());
        }
        notifyDataSetChanged();
    }


//    private void addUserItem(List<NewVote> newVoteList) {
//        NewVote item = new NewVote();
//        item.setUploadImages(newVoteList.get());
//        mItems.add(item);
//    }

    private void addPlanImageItem(String planName, List<UploadImage> images, String title) {
        NewVote item = new NewVote();
        item.setDescription(title);
        item.setUploadImages(images);
        item.setTitle(planName);
        mData.add(item);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

     class VotePlanImageViewHolder extends BaseViewHolder<NewVote>{
        private ItemNewVoteImageBinding mItemBinding;
        public VotePlanImageViewHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(NewVote item) {
            mItemBinding.tvPlanName.setText(StringUtils.filterNullAndTrim(item.getTitle()));
            mItemBinding.tvTitle.setText(item.getDescription());
            final List<UploadImage> images = item.getUploadImages();
            final ArrayList<String> urls = new ArrayList<String>();
            for (UploadImage image : images) {
                urls.add(image.getImageUrl());
            }
            mItemBinding.gridimageLayout.setImage(urls);

        }

    }
}
